.TH std::remove_cv,std::remove_const,std::remove_volatile 3 "2024.06.10" "http://cppreference.com" "C++ Standard Libary"
.SH NAME
std::remove_cv,std::remove_const,std::remove_volatile \- std::remove_cv,std::remove_const,std::remove_volatile

.SH Synopsis
   Defined in header <type_traits>
   template< class T >             \fB(1)\fP \fI(since C++11)\fP
   struct remove_cv;
   template< class T >             \fB(2)\fP \fI(since C++11)\fP
   struct remove_const;
   template< class T >             \fB(3)\fP \fI(since C++11)\fP
   struct remove_volatile;

   Provides the member typedef type which is the same as T, except that its topmost
   cv-qualifiers are removed.

   1) Removes the topmost const, or the topmost volatile, or both, if present.
   2) Removes the topmost const.
   3) Removes the topmost volatile.

   If the program adds specializations for any of the templates described on this page,
   the behavior is undefined.

.SH Member types

   Name Definition
   type the type T without cv-qualifier

.SH Helper types

   template< class T >                                           \fI(since C++14)\fP
   using remove_cv_t = typename remove_cv<T>::type;
   template< class T >                                           \fI(since C++14)\fP
   using remove_const_t = typename remove_const<T>::type;
   template< class T >                                           \fI(since C++14)\fP
   using remove_volatile_t = typename remove_volatile<T>::type;

.SH Possible implementation

   template<class T> struct remove_cv { typedef T type; };
   template<class T> struct remove_cv<const T> { typedef T type; };
   template<class T> struct remove_cv<volatile T> { typedef T type; };
   template<class T> struct remove_cv<const volatile T> { typedef T type; };

   template<class T> struct remove_const { typedef T type; };
   template<class T> struct remove_const<const T> { typedef T type; };

   template<class T> struct remove_volatile { typedef T type; };
   template<class T> struct remove_volatile<volatile T> { typedef T type; };

.SH Example

   Removing const/volatile from const volatile int* does not modify the type, because
   the pointer itself is neither const nor volatile.


// Run this code

 #include <type_traits>

 template<typename U, typename V>
 constexpr bool same = std::is_same_v<U, V>;

 static_assert
 (
     same<std::remove_cv_t<int>, int> &&
     same<std::remove_cv_t<const int>, int> &&
     same<std::remove_cv_t<volatile int>, int> &&
     same<std::remove_cv_t<const volatile int>, int> &&
     // remove_cv only works on types, not on pointers
     not same<std::remove_cv_t<const volatile int*>, int*> &&
     same<std::remove_cv_t<const volatile int*>, const volatile int*> &&
     same<std::remove_cv_t<const int* volatile>, const int*> &&
     same<std::remove_cv_t<int* const volatile>, int*>
 );

 int main() {}

.SH See also

   is_const     checks if a type is const-qualified
   \fI(C++11)\fP      \fI(class template)\fP
   is_volatile  checks if a type is volatile-qualified
   \fI(C++11)\fP      \fI(class template)\fP
   add_cv
   add_const
   add_volatile adds const and/or volatile specifiers to the given type
   \fI(C++11)\fP      \fI(class template)\fP
   \fI(C++11)\fP
   \fI(C++11)\fP
   remove_cvref combines std::remove_cv and std::remove_reference
   (C++20)      \fI(class template)\fP
